﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DimensionalMeasurement
{
    public partial class FormUserEdit : Form
    {
        /// <summary>
        /// 编辑用户的姓名
        /// </summary>
        private string name;
        /// <summary>
        /// 编辑用户的权限
        /// </summary>
        private Power power;
        public FormUserEdit(string name = "", Power power = Power.操作员)
        {
            InitializeComponent();
            //全局变量初始化
            this.name = name;
            this.power = power;
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FormUserEdit_Load(object sender, EventArgs e)
        {
            //只能赋予比登录用户低的权限
            for (int i = 0; i <= (int)PublicSet.userInfo.power; ++i)
            {
                cBPower.Items.Add((Power)i);
            }
            tBName.Text = name;
            cBPower.SelectedItem = power;
        }

        private void bOK_Click(object sender, EventArgs e)
        {
            if (tBName.Text == "" || tBName.Text == "格林晟")
            {
                MessageBox.Show("用户名填写不正确");
                return;
            }
            if (tBPassword.Text == "")
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            if (tBPassword.Text != tBVerifyPassword.Text)
            {
                MessageBox.Show("两次输入密码不一致");
                return;
            }
            if (name == "")
            {
                //增加用户
                if (!AddUserInfo(new UserInfo { name = tBName.Text, password = tBPassword.Text, power = (Power)cBPower.SelectedIndex }))
                {
                    MessageBox.Show("用户已存在");
                    return;
                }
            }
            else
            {
                //修改用户
                if (!UpdateUserInfo(new UserInfo { name = tBName.Text, password = tBPassword.Text, power = (Power)cBPower.SelectedIndex }, name))
                {
                    MessageBox.Show("与其他用户名冲突");
                    return;
                }
            }
            Close();
        }

        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="userInfo">增加的用户</param>
        /// <returns>增加是否成功</returns>
        public static bool AddUserInfo(UserInfo userInfo)
        {
            return MySQLHelper.ExecuteNonQuery(PublicSet.Connection, $"INSERT INTO tb_user(name, password, power)VALUES('{userInfo.name}', '{userInfo.password}', {(int)userInfo.power});");
        }
        /// <summary>
        /// 修改用户信息
        /// </summary>
        /// <param name="userInfo">修改后的用户信息</param>
        /// <param name="name">修改前的用户名</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateUserInfo(UserInfo userInfo, string name)
        {
            return MySQLHelper.ExecuteNonQuery(PublicSet.Connection, $"UPDATE tb_user SET name = '{userInfo.name}', password = '{userInfo.password}', power = {(int)userInfo.power} WHERE name = '{name}';");
        }


    }
}
